#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QString>
#include <QLineEdit>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    initTableWidget();
    connect(ui->name,&QLineEdit::selectionChanged,this,&Widget::clearText);
    connect(ui->tableWidget,&QTableWidget::itemSelectionChanged,this,&Widget::onItemSelectionChanged);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initTableWidget()
{
    ui->tableWidget->setRowCount(5);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    // ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems); // 点击项时选中项
    // 或
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); // 点击行时选中整行
    // 或
    // ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectColumns); // 点击列时选中整列
    QStringList headList;
    headList.append(("姓名"));
    headList.append(("年龄"));
    headList.append(("地址"));
    headList.append(("电话"));

    ui->tableWidget->setHorizontalHeaderLabels(headList);

    stus = new QList<Student>();
    Student stu;
    stu.setName("叶静");
    stu.setAge(34);
    stu.setAddr("广州");
    stu.setPhone("13560296641");

    stus->append(stu);



    this->refresh();
}

void Widget::aaa()
{

}


void Widget::on_btn_add_clicked()
{
    QString name = ui->name->text();
    QString age = ui->age->text();
    QString addr = ui->addr->text();
    QString phone = ui->phone->text();

    if(!name.isEmpty())
    {
        Student stu;
        stu.setName(name.toStdString());
        stu.setAge(age.toInt());
        stu.setAddr(addr.toStdString());
        stu.setPhone(phone.toStdString());
        stus->append(stu);
        this->refresh();
    }
}

void Widget::refresh()
{
    int idx = 0;
    for(auto stu : *stus)
    {
        ui->tableWidget->setItem(idx,0,new QTableWidgetItem(QString::fromUtf8(stu.getName())));
        ui->tableWidget->setItem(idx,1,new QTableWidgetItem(QString::number(stu.getAge())));
        ui->tableWidget->setItem(idx,2,new QTableWidgetItem(stu.getAddr().c_str()));
        ui->tableWidget->setItem(idx,3,new QTableWidgetItem(stu.getPhone().c_str()));
        ++idx;
    }
}

void Widget::clearText()
{
    QObject *senderObject = sender();
    if (senderObject) {
        // 检查senderObject的类型，或者根据需要进行其他操作
        QLineEdit *edt = (QLineEdit*)senderObject;
        edt->clear();
    }
}

void Widget::onItemClicked(QTableWidgetItem *item)
{

}

/**
 * 选中行触发
 * @brief Widget::onItemSelectionChanged
 */
void Widget::onItemSelectionChanged()
{
    this->selStu = new Student();
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    selStu->setName(items[0]->text().toStdString());
    selStu->setAge(items[1]->text().toInt());
    selStu->setAddr(items[2]->text().toStdString());
    selStu->setPhone(items[3]->text().toStdString());

    ui->name->setText(selStu->getName().c_str());
    ui->age->setText(QString::number(selStu->getAge()));
    ui->addr->setText(selStu->getAddr().c_str());
    ui->phone->setText(selStu->getPhone().c_str());
}

/**
 * 修改
 * @brief Widget::on_btn_edit_clicked
 */
void Widget::on_btn_edit_clicked()
{
    QString name = ui->name->text();
    QString age = ui->age->text();
    QString addr = ui->addr->text();
    QString phone = ui->phone->text();

    QList<Student>::Iterator it;
    for(it = stus->begin();it!=stus->end();it++)
    {
        if((*it).getName() == selStu->getName()){
            (*it).setName(name.toStdString());
            (*it).setAge(age.toInt());
            (*it).setAddr(addr.toStdString());
            (*it).setPhone(phone.toStdString());
        }
    }
    this->refresh();
}

